package cn.edu.zut.soft.student.controller.admin.teacher;

import cn.edu.zut.soft.student.bean.ResultBean;
import cn.edu.zut.soft.student.model.Teacher;
import cn.edu.zut.soft.student.model.enums.Role;
import cn.edu.zut.soft.student.service.teacher.TeacherBo;
import cn.edu.zut.soft.student.service.teacher.vo.TeacherQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/22 21:55
 */

@RestController
@RequestMapping("/admin/api/teacher")
public class TeacherController {
    @Autowired
    private TeacherBo teacherBo;

    @PostMapping("")
    public ResultBean<Integer> addTeacher(Teacher teacher) {
        teacher.setPassword(Role.teacher.getPassword());
        return new ResultBean<>(teacherBo.addTeacher(teacher));
    }

    @PostMapping("/import")
    public ResultBean<Integer> importStudents(MultipartFile file) throws Exception {
        return new ResultBean<>(teacherBo.importTeachers(file));
    }

    @DeleteMapping("/{teacherid}")
    public ResultBean<Integer> deleteTeacher(@PathVariable("teacherid") String teacherid) {
        return new ResultBean<>(teacherBo.deleteTeacher(teacherid));
    }

    @DeleteMapping("/list")
    public ResultBean<Integer> deleteTeachers(String allTeacherIds) {
        String[] ids = allTeacherIds.substring(1, allTeacherIds.length() - 1).split(",");
        for (String id : ids) {
            teacherBo.deleteTeacher(id);
        }
        return new ResultBean<>();
    }

    @PutMapping("")
    public ResultBean<Integer> updateTeacher(Teacher teacher) {
        return new ResultBean<>(teacherBo.updateTeacher(teacher));
    }

    @PutMapping("/{teacherid}")
    public ResultBean<Integer> resetPassword(@PathVariable("teacherid") String teacherid) {
        return new ResultBean<>(teacherBo.resetPassword(teacherid, Role.teacher.getPassword()));
    }

    @GetMapping("/{teacherid}")
    public ResultBean<Teacher> queryTeacher(@PathVariable("teacherid") String teacherid) {
        return new ResultBean<>(teacherBo.queryTeacherById(teacherid));
    }

    @GetMapping("")
    public ResultBean<List<Teacher>> queryTeachers(TeacherQuery teacherQuery) {
        return new ResultBean<>(teacherBo.queryTeachers(teacherQuery));
    }

}
